package com.cnlbc.View;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.cnlbc.Dao.BookDao;
import com.cnlbc.Dao.BookTypeDao;
import com.cnlbc.Model.BookType;
import com.cnlbc.Util.DbUtil;
import com.cnlbc.Util.StringUtil;
import java.awt.Font;

public class ListType extends JInternalFrame {
	private JTable bookTypeTable;
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private BookDao bookDao=new BookDao();
	private JTextField booktype;
	private JTextField id;
	private JTextField bookTypename;
	private JTextArea bookTypedesc;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ListType frame = new ListType();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ListType() {
		setTitle("图书类别管理");
		setIconifiable(true);
		setClosable(true);
		setBounds(100, 100, 495, 491);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JLabel lblNewLabel = new JLabel("图书类别名称 ");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 18));
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			bookTypeSearchAction(e);
			}
		});
		
		booktype = new JTextField();
		booktype.setFont(new Font("宋体", Font.PLAIN, 16));
		booktype.setColumns(10);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(77)
					.addComponent(lblNewLabel)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(booktype, 145, 145, 145)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnNewButton)
					.addGap(82))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(32)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE)
						.addComponent(scrollPane, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 397, Short.MAX_VALUE))
					.addGap(50))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(34)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel)
						.addComponent(booktype, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNewButton))
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 197, Short.MAX_VALUE)
					.addContainerGap())
		);
		
		JLabel lblNewLabel_1 = new JLabel("编号");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 16));
		
		id = new JTextField();
		id.setEditable(false);
		id.setText("");
		id.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("图书类别名称");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookTypename = new JTextField();
		bookTypename.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("描述");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 16));
		
		bookTypedesc = new JTextArea();
		
		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookTypeUpdata(e);
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ListType.class.getResource("/images/modify.png")));
		
		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.setFont(new Font("宋体", Font.PLAIN, 18));
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deteleTypeAction(e);
			}
		});
		btnNewButton_2.setIcon(new ImageIcon(ListType.class.getResource("/images/delete.png")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_1)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(id, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
									.addComponent(lblNewLabel_2)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookTypename, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_panel.createSequentialGroup()
									.addComponent(lblNewLabel_3)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookTypedesc, GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)))
							.addContainerGap())
						.addGroup(Alignment.TRAILING, gl_panel.createSequentialGroup()
							.addComponent(btnNewButton_1)
							.addGap(76)
							.addComponent(btnNewButton_2)
							.addGap(64))))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_1)
						.addComponent(id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2)
						.addComponent(bookTypename, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNewLabel_3)
						.addComponent(bookTypedesc, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnNewButton_2)
						.addComponent(btnNewButton_1))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		bookTypedesc.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
		
		bookTypeTable = new JTable();
		bookTypeTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				bookTypemousePressed(e);
				}
		});
		bookTypeTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u56FE\u4E66\u7C7B\u522B\u540D\u79F0", "\u56FE\u4E66\u7C7B\u522B\u63CF\u8FF0"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTypeTable.getColumnModel().getColumn(1).setPreferredWidth(123);
		bookTypeTable.getColumnModel().getColumn(2).setPreferredWidth(176);
		scrollPane.setViewportView(bookTypeTable);
		getContentPane().setLayout(groupLayout);

		this.fillTable(new BookType());
	}
	/**
	 * 修改数据
	 * @param e
	 */
	private void bookTypeUpdata(ActionEvent e) {
		String i=id.getText();
		String bookTypeName=bookTypename.getText();
		String bookTypeDesc=bookTypedesc.getText();
		if(StringUtil.isEmpty(i)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的数据");
			return;
		}
		if(StringUtil.isEmpty(bookTypeName)){
			JOptionPane.showMessageDialog(null, "图书类别名称不能为空");
			return;
		}
		BookType bookType=new BookType(Integer.parseInt(i),bookTypeName,bookTypeDesc);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int modifNum=bookTypeDao.updata(con, bookType);
			if(modifNum==1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.fillTable(new BookType());
			}else {
				JOptionPane.showMessageDialog(null,"修改失败");
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null,"修改失败");
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
	}

	/**
	 * 删除图书类型
	 * @param e
	 */
	private void deteleTypeAction(ActionEvent evt) {
		// TODO Auto-generated method stub
		String i=this.id.getText();
		if(StringUtil.isEmpty(i)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的数据");
			return;
		}
		int t=JOptionPane.showConfirmDialog(null, "确定要删除这条数据么");
		if(t==0) {
		Connection con=null;
		try {
			con=dbUtil.getCon();
			if(bookDao.exitBookByBookTypeId(con, i)) {
				JOptionPane.showMessageDialog(null, "类别下有图书，不能删除");
				return;
			}
			int n=bookTypeDao.delete(con, i);
			if(n==1) {
				JOptionPane.showMessageDialog(null, "删除成功");
				this.resetValue();
				this.fillTable(new BookType());
			}else {
				JOptionPane.showMessageDialog(null, "删除失败");
			}	
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
	}
	/**
	 * 重置
	 */
	private void resetValue() {
		// TODO Auto-generated method stub
		this.id.setText("");
		this.bookTypename.setText("");
		this.bookTypedesc.setText("");
	}

	/**
	 * 表格点击事件
	 * @param evt
	 */
	private void bookTypemousePressed(MouseEvent evt) {
		int row=bookTypeTable.getSelectedRow();
		Object c=bookTypeTable.getValueAt(row,0);
		String str=c.toString();
		id.setText(str);
		bookTypename.setText((String)bookTypeTable.getValueAt(row, 1));
		bookTypedesc.setText((String)bookTypeTable.getValueAt(row, 2));
	}

	/**
	 * 初始化表格
	 * @param bookType
	 */
	private void fillTable(BookType bookType) {
		DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel();
		dtm.setRowCount(0);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con, bookType);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getInt("id"));
				v.add(rs.getString("bookTypeName"));
				v.add(rs.getString("bookTypeDesc"));
				dtm.addRow(v);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				dbUtil.close(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
/**
 * 图书类别查询
 * @param evt
 */
	private void bookTypeSearchAction(ActionEvent evt) {
		// TODO Auto-generated method stub
		String sname=this.booktype.getText();
		BookType bookType=new BookType();
		bookType.setBookTypeName(sname);
		this.fillTable(bookType);
	}
}
